#序列化器
from rest_framework import serializers
from .models import Goods,AdminProfile,AdminLog

class GoodsSerializer(serializers.ModelSerializer):
  image_url = serializers.SerializerMethodField()
  class Meta:
    model=Goods
    fields = ['id', 'title', 'subtitle', 'price', 'original_price', 'image_url', 'detail_url', 'description']
  def get_image_url(self,obj):
    request = self.context.get('request')
    if obj.image:
      return request.build_absolute_uri(obj.image.url)
    return None

class AdminProfileSerializer(serializers.ModelSerializer):
  avatar_url = serializers.SerializerMethodField()
  class Meta:
    model=AdminProfile
    fields = ['id', 'avatar_url', 'email', 'phone', 'department']
  def get_avatar_url(self,obj):
    request = self.context.get('request')
    if obj.avatar:
      return request.build_absolute_uri(obj.avatar.url)
    return None

class AdminLogSerializer(serializers.ModelSerializer):
  class Meta:
    model=AdminLog
    fields = ['id', 'action', 'description', 'ip_address', 'create_at']
  
